unit uMyXml;

interface

uses
  Classes, XMLIntf, msxmldom, XMLDoc, xmldom, SysUtils, msxml, dialogs;

type
  Pcate_rec = ^Tcate_rec;
  Tcate_rec = record
    ID   : string;
    Name : string;
  end;

  Pprof_rec = ^Tprof_rec;
  Tprof_rec = record
    FatherID : string;
    ID   : string;
    Name : string;
    Comment : string;
  end;

Type
  TMyXML = class

  private
    myxml: TXMLDocument;
    Fprofilelinks: TList;
    Fcategories: Tlist;

    procedure init();
    procedure Setcategories(const Value: TList);
    procedure Setprofilelinks(const Value: TList);

    procedure set_profilelinks();
    procedure set_categories();

  public
    constructor create(filepath: string);
    function get_name(fatherID, ID: string):string;

    property categories: TList read Fcategories write Setcategories;
    property profilelinks: TList read Fprofilelinks write Setprofilelinks;

  end;

implementation

{ TMyXML }

constructor TMyxml.create(filepath:string);
begin
  myxml := TXMLDocument.Create(filepath);
  Fcategories := TList.Create;
  Fprofilelinks := Tlist.Create;
  init();
end;

procedure TMyxml.set_profilelinks();
var
  node, temp_node, prof_node: IDOMNode;
  i, j : Integer;
  p : Pprof_rec;
  father_ID: string;
begin
  node := myxml.DOMDocument.documentElement;

  for i := 0 to node.childNodes.length-1 do
  begin
    temp_node := node.childNodes[i];
    father_ID := temp_node.attributes.getNamedItem('ID').nodeValue;

    for j:=0 to temp_node.childNodes.length-1 do
    begin
      prof_node := temp_node.childNodes[j];

      New(p);
      p.FatherID := father_ID;
      p.ID := prof_node.attributes.getNamedItem('ID').nodeValue;
      p.Name := prof_node.attributes.getNamedItem('Name').nodeValue;
      p.Comment := prof_node.attributes.getNamedItem('Comment').nodeValue;

      Fprofilelinks.Add(p);
    end;
  end;
end;

procedure TMyxml.set_categories();
var
  node, temp_node: IDOMNode;
  i : Integer;
  p : Pcate_rec;
begin
  node := myxml.DOMDocument.documentElement;

  for i := 0 to node.childNodes.length-1 do
  begin
    temp_node := node.childNodes[i];

    New(p);
    p.ID := temp_node.attributes.getNamedItem('ID').nodeValue;
    p.Name := temp_node.attributes.getNamedItem('Name').nodeValue;

    Fcategories.Add(p);
  end;


end;

procedure TMyxml.init();
begin
  set_categories();
  set_profilelinks();
end;



procedure TMyXML.Setcategories(const Value: TList);
begin
  Fcategories := Value;
end;

procedure TMyXML.Setprofilelinks(const Value: TList);
begin
  Fprofilelinks := Value;
end;

function TMyXML.get_name(fatherID, ID: string):string;
var
  node, temp_node, prof_node: IDOMNode;
  i, j : Integer;
  p : Pprof_rec;
  father_ID: string;
begin
  node := myxml.DOMDocument.documentElement;

  for i := 0 to node.childNodes.length-1 do
  begin
    temp_node := node.childNodes[i];
    if temp_node.attributes.getNamedItem('ID').nodeValue<>fatherID then
      Continue;

    for j:=0 to temp_node.childNodes.length-1 do
    begin
      prof_node := temp_node.childNodes[j];

      if prof_node.attributes.getNamedItem('ID').nodeValue<>ID then
        Continue;

      result := prof_node.attributes.getNamedItem('Name').nodeValue;
      Exit;
    end;
  end;
end;

end.
